package com.ruoyi.web.controller.depot;

import java.math.BigDecimal;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.depot.domain.WalmartStockPlan;
import com.ruoyi.depot.domain.WalmartStockPlanPackingList;
import com.ruoyi.depot.mapper.OutboundStockLogMapper;
import com.ruoyi.depot.mapper.WalmartStockProductDetailsMapper;
import com.ruoyi.sale.domain.*;
import com.ruoyi.depot.service.IWalmartStockPlanService;
import com.ruoyi.walmart.domain.shipments.dto.ShipmentStockFilters;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * walmart备货计划Controller
 *
 * @author ruoyi
 * @date 2025-02-26
 */
@RestController
@RequestMapping("/walmart/stock/plan")
public class WalmartStockPlanController extends BaseController {
	@Autowired
	private IWalmartStockPlanService walmartStockPlanService;

	@Autowired
	private WalmartStockProductDetailsMapper walmartStockProductDetailsMapper;

	@Autowired
	private OutboundStockLogMapper outboundStockLogMapper;

	/**
	 * 查询walmart备货计划列表
	 */
	@PreAuthorize("@ss.hasPermi('depot:stock:list')")
	@GetMapping("/list")
	public TableDataInfo list(WalmartStockPlan walmartStockPlan) {
		startPage();
		List<WalmartStockPlan> list = walmartStockPlanService.selectWalmartStockPlanList(walmartStockPlan);
		return getDataTable(list);
	}

	/**
	 * 导出walmart备货计划列表
	 */
	@PreAuthorize("@ss.hasPermi('depot:stock:export')")
	@Log(title = "walmart备货计划", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, WalmartStockPlan walmartStockPlan) {
		List<WalmartStockPlan> list = walmartStockPlanService.selectWalmartStockPlanList(walmartStockPlan);
		ExcelUtil<WalmartStockPlan> util = new ExcelUtil<WalmartStockPlan>(WalmartStockPlan.class);
		util.exportExcel(response, list, "walmart备货计划数据");
	}

	/**
	 * 获取walmart备货计划详细信息
	 */
	@PreAuthorize("@ss.hasPermi('depot:stock:query')")
	@GetMapping(value = "/{stockId}")
	public AjaxResult getInfo(@PathVariable("stockId") String stockId) {
		return success(walmartStockPlanService.selectWalmartStockPlanByStockId(stockId));
	}

	/**
	 * 新增walmart备货计划
	 */
	@PreAuthorize("@ss.hasPermi('depot:stock:add')")
	@Log(title = "walmart备货计划", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Plan plan) {
		return toAjax(walmartStockPlanService.insertWalmartStockPlan(plan));
	}

	/**
	 * 修改walmart备货计划
	 */
	@PreAuthorize("@ss.hasPermi('depot:stock:edit')")
	@Log(title = "walmart备货计划", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody WalmartStockPlan walmartStockPlan) {
		return toAjax(walmartStockPlanService.updateWalmartStockPlan(walmartStockPlan));
	}

	/**
	 * 删除walmart备货计划
	 */
	@PreAuthorize("@ss.hasPermi('depot:stock:remove')")
	@Log(title = "walmart备货计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stockIds}")
	public AjaxResult remove(@PathVariable String[] stockIds) {
		return toAjax(walmartStockPlanService.deleteWalmartStockPlanByStockIds(stockIds));
	}

	/**
	 * walmart备货计划发货
	 */
	@PreAuthorize("@ss.hasPermi('depot:stock:shipping')")
	@PutMapping("/shippingPlan")
	public AjaxResult shippingStockPlan(@RequestBody String[] stockIds) {
		return toAjax(walmartStockPlanService.shippingStockPlan(stockIds));
	}

	/**
	 * 导入walmart备货计划
	 */
	@PreAuthorize("@ss.hasPermi('depot:stock:import')")
	@PostMapping("/import/template")
	public AjaxResult importExcel(@RequestBody List<WalmartStockPlanPackingList> planPackingLists){
		if (planPackingLists.isEmpty()) {
			throw new ServiceException("导入的数据为空，请检查!");
		}

		int result = walmartStockPlanService.importExcel(planPackingLists);
		return toAjax(result);
	}

	/**
	 * 导入walmart备货计划运费
	 */
	@PreAuthorize("@ss.hasPermi('depot:stock:importFreight')")
	@PostMapping("/importFreight")
	public AjaxResult importFreight(@RequestBody List<WalmartStockPlanPackingList> planPackingLists,
									@RequestParam BigDecimal totalAmount,
									@RequestParam int calculationMode) {
		if (planPackingLists.isEmpty()) {
			throw new ServiceException("导入的数据为空，请检查!");
		}
		int result = walmartStockPlanService.importFreight(planPackingLists,totalAmount,calculationMode);
		return toAjax(result);
	}
//	@PreAuthorize("@ss.hasPermi('system:plan:import')")
//	@PostMapping("/import/template")
//	public AjaxResult importExcel(@RequestParam("file") MultipartFile file,
//								  @RequestParam(value = "stockId", required = false) String stockId) throws IOException {
//		if (file.isEmpty()) {
//			throw new ServiceException("请上传 Excel 文件！");
//		}
//		if(stockId.isEmpty()){
//			throw new ServiceException("缺少导入备货清单的备货计划编号！");
//		}
//		int result = walmartStockPlanService.importExcel(file,stockId);
//		return toAjax(result);
//	}

	/**
	 * 获取walmart备货计划sku信息
	 */
//	@PreAuthorize("@ss.hasPermi('system:plan:query')")
	@GetMapping(value = "/skus/{stockId}")
	public AjaxResult selectSku(@PathVariable("stockId") String stockId) {
		return success(walmartStockPlanService.selectSku(stockId));
	}

	/**
	 * 获取walmart备货计划gtin信息
	 */
//	@PreAuthorize("@ss.hasPermi('system:plan:query')")
	@GetMapping(value = "/gtins/{stockId}")
	public AjaxResult selectGTIN(@PathVariable("stockId") String stockId) {
		return success(walmartStockPlanService.selectGTIN(stockId));
	}

	/**
	 * 获取walmart备货计划导入的备货清单信息
	 */
//	@PreAuthorize("@ss.hasPermi('system:plan:query')")
	@GetMapping(value = "/importedPlan/{stockId}")
	public AjaxResult getImportedPlan(@PathVariable("stockId") String stockId) {
		return success(walmartStockPlanService.getImportedPlan(stockId));
	}

	/**
	 * 修改备货计划物流公司
	 */
	@PreAuthorize("@ss.hasPermi('depot:stock:updateStockPlanLogistics')")
	@PostMapping("/updateStockPlanLogistics")
	public AjaxResult updateStockPlanLogistics(@RequestBody WalmartStockPlan walmartStockPlan) {
		if(walmartStockPlan.getStockId() == null || walmartStockPlan.getLogisticsCompany() == null){
			throw new ServiceException("修改的备货计划id或物流公司不存在");
		}
		int result = walmartStockPlanService.updateStockPlanLogistics(walmartStockPlan);
		return toAjax(result);
	}

	/**
	 * 修改备货计划体积计算模式
	 */
	@PreAuthorize("@ss.hasPermi('depot:stock:updateStockPlanVolumeMode')")
	@PostMapping("/updateStockPlanVolumeMode")
	public AjaxResult updateStockPlanVolumeMode(@RequestBody WalmartStockPlan walmartStockPlan) {
		if(walmartStockPlan.getStockId() == null || walmartStockPlan.getVolumeMode() == null){
			throw new ServiceException("修改的备货计划id或体积计算模式不存在");
		}
		int result = walmartStockPlanService.updateStockPlanVolumeMode(walmartStockPlan);
		return toAjax(result);
	}

	/**
	 * 修改备货计划快递单号
	 */
	@PreAuthorize("@ss.hasPermi('depot:stock:updateStockTrackingNumber')")
	@PostMapping("/updateStockTrackingNumber")
	public AjaxResult updateStockTrackingNumber(@RequestBody WalmartStockPlan walmartStockPlan) {
		if(walmartStockPlan.getStockId() == null || walmartStockPlan.getTrackingNumber() == null){
			throw new ServiceException("修改的备货计划id或快递单号不存在");
		}
		int result = walmartStockPlanService.updateStockTrackingNumber(walmartStockPlan);
		return toAjax(result);
	}

    /**
     * 获取walmart备货计划清单信息
     */
//	@PreAuthorize("@ss.hasPermi('system:plan:query')")
    @GetMapping(value = "/getStockProductDetails/{stockId}")
    public AjaxResult getStockProductDetails(@PathVariable("stockId") String stockId) {
		String[] stockIds = new String[]{stockId};
        return success(walmartStockProductDetailsMapper.selectListByStockIds(stockIds));
    }

	/**
	 * 手动绑定备货计划
	 * @param ShipmentStockFilters
	 * @return
	 */
	@PostMapping("/bingShipmentStock")
	public AjaxResult bingShipmentStock(@RequestBody ShipmentStockFilters ShipmentStockFilters) {

		if(ShipmentStockFilters.getStockId().equals("")){
			throw new ServiceException("获取备货计划ID失败" );
		}
		if(ShipmentStockFilters.getShipmentId().equals("")){
			throw new ServiceException("获取ShipmentId失败");
		}
		walmartStockPlanService.bingShipmentStock(ShipmentStockFilters);
		return null;
	}
}
